<?php
require_once 'inc_security.php';
$action = getValue('action','str','POST','');
switch($action){
    case 'search_relate':
        $keyword = getValue('keyword','str','POST','');
        if(!$keyword){
            break;
        }
        $str = '';
        $db_se = new db_query('SELECT tut_id,tut_title FROM tutorials
                                WHERE tut_active = 1 AND (tut_title LIKE "%'.$keyword.'%" OR tut_tags LIKE "%'.$keyword.'%")
                                ORDER BY tut_title ASC
                                LIMIT 10 ' );
        $result = $db_se->resultArray();unset($db_se);
        $result = array_unique($result,SORT_REGULAR);
        if($result){
            foreach($result as $lir){
                $str .= '
                    <li>
                        <label class="checkbox">
                            <input type="checkbox" title="'.$lir['tut_title'].'" value="'.$lir['tut_id'].'" class="tut_relate_result" id="search_news_relate_'.$lir['tut_id'].'"/>
                            '.$lir['tut_title'].'
                        </label>
                    </li>';
            }
            $str .= '<span class="label label-info" style="cursor:pointer" onclick="Tut_add_relate()">Thêm vào</span>';
        }
        echo $str;
    break;
    case 'update_image_alt':
       $rt = array();
       $rt['success'] = 0;
       $imu_id = getValue('imu_id','int','POST',0);
       $image_alt = getValue('image_alt','str','POST','');
       db_nonQuery("UPDATE images_upload SET imu_image_alt ='".$image_alt."' WHERE imu_id =".$imu_id);
       $rt['success'] = 1;
       echo json_encode($rt); 
    break;
    case 'update_image_name':
       $rt = array();
       $rt['success'] = 0;
       $imu_id = getValue('imu_id','int','POST',0);
       $image_name_new = getValue('image_name','str','POST','');
       $image_name_old = getValue('image_name_old','str','POST','');
       $img_link = $_SERVER["DOCUMENT_ROOT"].'/pictures/'.$bg_table.'/'.$image_name_old;
       $img_link_new = $_SERVER["DOCUMENT_ROOT"].'/pictures/'.$bg_table.'/'.$image_name_new;
       $src = '/pictures/'.$bg_table.'/'.$image_name_old;
       $src_new = '/pictures/'.$bg_table.'/'.$image_name_new;
       rename($img_link,$img_link_new);
       $search = db_array("SELECT ser_id,ser_detail FROM services WHERE ser_detail LIKE '%".$src."%'");       
       foreach($search as $row){
         $detail_old = $row['ser_detail'];
         $detail_new = str_replace($src,$src_new,$row['ser_detail']);
         db_nonQuery("UPDATE services SET ser_detail ='".addslashes($detail_new)."' WHERE ser_id =".$row['ser_id']);//update lại dữ liệu chi tiết         
       }
       db_nonQuery("UPDATE images_upload SET imu_image ='".$image_name_new."' WHERE imu_id =".$imu_id);
       $rt['success'] = 1;
       echo json_encode($rt); 
    break;
}
?>